package com.masterclient.app.modules.aftersale;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.aftersale.ExpressBean;
import com.eoner.baselibrary.bean.aftersale.SendLogisticInfoBean;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.aftersale.adapter.SendLogisticAdapter;
import com.masterclient.app.modules.aftersale.contract.SendLogisticInfoContract;
import com.masterclient.app.modules.aftersale.contract.SendLogisticInfoPresenter;
import com.eoner.uikit.header.MyRefreshHead;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AroutePath.SEND_LOGISTIC_INFO_FRAGMENT)
public class SendLogisticInfoFragment extends BaseFragment<SendLogisticInfoPresenter> implements SendLogisticInfoContract.View {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Autowired(name = "order_id")
    String orderId;
    @Autowired(name = "biz_type")
    String bizType;

    private SendLogisticAdapter sendLogisticAdapter;

    public static SendLogisticInfoFragment newInstance() {
        return new SendLogisticInfoFragment();
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_send_logistic_info, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();
        ARouter.getInstance().inject(this);

        refreshLayout.setRefreshHeader(new MyRefreshHead(getActivity()));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        sendLogisticAdapter = new SendLogisticAdapter(new SendLogisticAdapter.OnClickItemListener() {
            @Override
            public void clickItem(String id) {
                startActivitryWithBundle(LogistInfoActivity.class, new String[][]{{"order_id", orderId},{"track_no", id},{"biz_type", bizType}});
            }

            @Override
            public void clickUpdate(int position) {
                ExpressBean bean = sendLogisticAdapter.getData().get(position);
                Intent intent = new Intent(getActivity(),AddReturnLogistActivity.class);
                intent.putExtra("refund_id",bean.id);
                intent.putExtra("type","order");
                intent.putExtra("update","1");
                intent.putExtra("shipment_company",bean.shipmentCompany);
                intent.putExtra("track_no",bean.trackNo);
                intent.putExtra("shipment_code", bean.carrierCode);
                startActivityForResult(intent, 0x127);
//                startActivitryWithBundle(AddReturnLogistActivity.class,new String[][]{{"refund_id",bean.id}
//                        ,{"type","order"},{"update","1"},{"shipment_company",bean.shipmentCompany}
//                        ,{"track_no",bean.trackNo},{"shipment_code", bean.carrierCode}});
            }
        });
        recyclerView.setAdapter(sendLogisticAdapter);
        sendLogisticAdapter.bindToRecyclerView(recyclerView);

        mPresenter.getSendLogisticInfo(orderId,bizType);
        refreshLayout.setOnRefreshListener( refresh -> {
            mPresenter.getSendLogisticInfo(orderId,bizType);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (refreshLayout != null) refreshLayout.finishRefresh();
        if (requestCode == 0x127 && resultCode == Activity.RESULT_OK){
            mPresenter.getSendLogisticInfo(orderId,bizType);
        }
    }

    @OnClick({R.id.rl_back})
    void onClick(View view) {
        if (view.getId() == R.id.rl_back) {
            Objects.requireNonNull(getActivity()).finish();
        }
    }

    @Override
    public void showSendLogisticInfo(SendLogisticInfoBean bean) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }
        if (bean != null && bean.expressList != null && bean.expressList.size() > 0) {
            sendLogisticAdapter.setCanUpdate("1".equals(bean.editShipment));
            sendLogisticAdapter.setNewData(bean.expressList);
        }
    }

    @Override
    public void showFail(String msg) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh(false);
        }
        ToastUtil.showToast(msg);
    }
}
